<?php

/**
 * company
 * @version 1.0
 * @package application
 * @subpackage application/controllers/company/
 */
class Company extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('company_model');
        $this->_rule_config = array(
            array('field' => 'title', 'label' => '标题', 'rules' => 'required'),
            array('field' => 'content', 'label' => '内容', 'rules' => 'required'),
        );
    }

    /**
     * index 显示列表页
     * @author 
     */
    public function index() {
        $attributes = $this->input->get();
        $attributes['title_like'] = $attributes['title'];
        unset($attributes['title']);
        $search['attributes'] = $attributes;
        // 总数与分页
        $result = $this->company_model->all($search);
        $data['total'] = count($result);
        // 请使用config_item( 'per_page' )获取全局显示条数
        $per_page = config_item('per_page');
        $this->load->library('pagination');
        $pagination_config = array(
            'base_url' => base_url($this->_site_path . '/company/index'),
            'total_rows' => count($result),
            'per_page' => $per_page,
            'uri_segment' => 4,
        );
        $this->pagination->initialize($pagination_config);
        $data['pagination'] = $this->pagination->create_links();

        $search['limit'] = array('persize' => $per_page, 'offset' => $this->pagination->get_cur_offset());
        $data['company_data'] = $this->company_model->all($search);

        $this->load->view($this->_site_path . '/company/index', $data);
    }

    /**
     * view 显示详细页
     * @param $id 主键值
     * @author 
     */
    // public function view($id)
    // {
    // 	$data['company_data']=$this->company_model->get_by_pk($id);
    // 	$this->load->view($this->_site_path.'/company/view',$data);
    // }

    /**
     * add 添加 
     * @author 
     */
    public function add() {
        $data = array();
        $data['is_edit'] = FALSE;
        if ($this->input->post('is_submit') && $this->form_validation->check_token()) {
            try {
                $company_data = $this->input->post();
                $new_data = $this->get_insert_data($company_data);
                if ($new_data['flag'] == 0)
                    throw new Exception($new_data['imagemark'], 0);
                unset($new_data['flag']);
                $company_data['created'] = $company_data['modified'] = time();
                if ($company_data['author'] == "") {
                    $company_data['author'] = $this->auth->get_user('user_name');
                }
                $company_data['joinip'] = $this->session->userdata('ip_address');
                $company_data = array_merge($company_data, $new_data);
                // exit();
                $data['company_data'] = $company_data;
                //设置验证规则
                $this->form_validation->set_rules($this->_rule_config);
                if (!$this->form_validation->run())
                    throw new Exception(validation_errors(), 0);
                //插入
                $company_id = $this->company_model->insert($company_data);
                init_messagebox('添加成功', 'success', 1, base_url($this->_site_path . '/company/index'));
            } catch (Exception $e) {
                init_messagebox($e->getMessage(), 'error', $e->getCode());
            }
        }
        
        $this->load->view($this->_site_path . '/company/form', $data);
    }

    /**
     * edit 编辑 
     * @param $id 主键值
     * @author 
     */
    public function edit($id) {
        $data = array();
        $data['is_edit'] = TRUE;
        $data['company_data'] = $this->company_model->get_by_pk($id);
        if ($this->input->post('is_submit') && $this->form_validation->check_token()) {
            try {
                $company_data = $this->input->post();
                $new_data = $this->get_insert_data($company_data);
                if ($new_data['flag'] == 0)
                    throw new Exception($new_data['imagemark'], 0);
                unset($new_data['flag']);
                $company_data['created'] = time();
                $company_data['depth'] = $this->auth->get_user('depth');
                $company_data['joinip'] = $this->session->userdata('ip_address');
                $company_data = array_merge($company_data, $new_data);
                $data['company_data'] = $company_data;
                //设置验证规则
                $this->form_validation->set_rules($this->_rule_config);
                if (!$this->form_validation->run())
                    throw new Exception(validation_errors(), 0);
                //修改
                $this->company_model->update_by_pk($company_data, $id);
                init_messagebox('修改成功', 'success', 1, base_url($this->_site_path . '/company/index'));
            } catch (Exception $e) {
                init_messagebox($e->getMessage(), 'error', $e->getCode());
            }
        }

        $this->load->view($this->_site_path . '/company/form', $data);
    }

    /**
     * delete 删除
     * @param $id 主键值
     * @author 
     */
    public function delete($id) {
        $this->company_model->delete_by_pk($id);
        redirect(base_url($this->_site_path . '/company/index'));
    }

    /**
     * delete_all 批量删除
     * @param $id 主键值
     * @author 
     */
    public function delete_all() {
        $ids = $this->input->post('ids');
        foreach ($ids as $id) {
            $this->company_model->delete_by_pk($id);
        }
        redirect(base_url($this->_site_path . '/company/index'));
    }

    /**
     * 文件上传函数
     * @param  [string] $field [文件域的名称]
     * @return [array]        [上传文件后返回的结果数组]
     */
    public function upload_file($field) {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
//        $config['max_size'] = '1000';
//        $config['max_width'] = '1000';
//        $config['max_height'] = '1000';
        $config['file_name'] = date("YmdHis", time());
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($field)) {
            $result = array('flag' => 0, 'msg' => $this->upload->display_errors());
        } else {
            $data = $this->upload->data();
            $result = array('flag' => 1, 'msg' => substr($config['upload_path'], 1) . $data['file_name']);
        }
        return $result;
    }

    /**
     * 获取插入到数据库中的通用数据
     * @param  [array] [post提交的数据]
     * @return [null]
     */
    public function get_insert_data($company_data) {
        if ($_FILES['imagemark']['name'] != '') {
            $result = $this->upload_file('imagemark');
            $company_data['flag'] = $result['flag'];
            $company_data['imagemark'] = $result['msg'];
        } else {
            $company_data['flag'] = 2;
            $company_data['imagemark'] = "";
        }
        return $company_data;
    }

}
